﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Menu_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Dim level As String = -1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


            Dim ctrl As System.Web.UI.WebControls.WebControl
            ctrl = txtDescription.CreateCommandButton("ask_mask_fullname", "remove_format.gif", "Bỏ định dạng")
            ctrl.Attributes("onclick") = "removeTextFormat('" + txtDescription.ClientID + "');"
            'get the pos after the Italic    
            Dim pos As Integer
            pos = txtDescription.ToolControls.IndexOf("JustifyRight") + 1
            'add this custom button into the editor    
            txtDescription.InsertToolControl(pos, "ask_mask_fullname", ctrl)


            If Not Request.QueryString("Menuid").ToString.Equals("0") Then
                getParent()
                hidID.Value = Request.QueryString("Menuid")
                ShowData()
            End If
        End If

    End Sub
    Protected Sub getParent()
        drlParent.Items.Clear()
        Dim data As New VSEntities
        Dim p As List(Of Menu) = (From q In data.Menus Select q).ToList
        drlParent.DataValueField = "MenuID"
        drlParent.DataTextField = "Title"
        RecursiveFillTree(p, 0)
        drlParent.Items.Insert(0, New ListItem("-Chọn menu cấp cha-", "0"))

    End Sub
    Private Sub RecursiveFillTree(ByVal dtParent As List(Of Menu), ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Dim data As New VSEntities
        Dim dv As List(Of Menu) = (From q In data.Menus Where q.ParentId = parentID Select q).ToList
        Dim i As Integer

        If dv.Count > 0 Then
            For i = 0 To dv.Count - 1
                drlParent.Items.Add(New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Title.ToString()), dv.Item(i).MenuId.ToString()))
                RecursiveFillTree(dtParent, Integer.Parse(dv.Item(i).MenuId.ToString()))
            Next
        End If

        level -= 1
        'on the each function end level will decrement by 1
    End Sub

    Protected Sub ShowData()
        Dim data As New VSEntities
        Dim p As Menu = (From q In Data.Menus Where q.MenuId = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then
            txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
            txtMetaTitle.Text = IIf(IsNothing(p.TitlePage) = True, "", p.TitlePage)
            hidImagePath.Text = IIf(IsNothing(p.ImageLogo) = True, "", p.ImageLogo)
            imgUpload.ImageUrl = IIf(IsNothing(p.ImageLogo) = True, "", p.ImageLogo)
            txtDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
            drlMenuposition.SelectedValue = IIf(IsNothing(p.MenuPosition) = True, "", p.MenuPosition)
            drlParent.SelectedValue = IIf(IsNothing(p.ParentId) = True, "", p.ParentId)

        End If
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim data As New VSEntities
        Dim p As Menu = (From q In Data.Menus Where q.MenuId = hidID.Value).SingleOrDefault
        Try
            p.MenuId = hidID.Value
            p.Title = txtTitle.Text.Trim()

            p.TitlePage = txtMetaTitle.Text.Trim
            p.ImageLogo = hidImagePath.Text.Trim
            p.Description = txtDescription.Text
            p.MenuPosition = CInt(drlMenuposition.SelectedValue)
            p.Modified = Date.Now
            p.Modifier = Session("UserName")
            If (p.ParentId <> drlParent.SelectedValue) Then
                Dim parent As Integer = CInt(drlParent.SelectedValue)
                Dim t = (From h In data.Menus Where h.ParentId = parent Order By h.Sort Descending Select h).FirstOrDefault
                If Not t Is Nothing Then
                    p.Sort = CInt(t.Sort.ToString) + 1
                Else
                    p.Sort = 1
                End If
            End If
            p.ParentId = CInt(drlParent.SelectedValue)
            p.QueryString = makeURLFriendly(txtTitle.Text.Trim)
            data.SaveChanges()
            Insert_App_Log("Update Menu:" & txtTitle.Text.Trim & "", Function_Name.Menu, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Menu/List.aspx?MenuId=" & drlParent.SelectedValue.ToString & "';", Me.Page, True)
            'Response.Redirect("List.aspx?MenuId=" + drlParent.SelectedValue.ToString)
        Catch ex As Exception
            log4net.Config.XmlConfigurator.Configure()
            log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End Try
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region


End Class
